//
//  CarNewsBase.m
//  我的小汽车
//
//  Created by 邹壮壮29 on 15-1-7.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "CarNewsBase.h"
#import "CarNewsModel.h"
#import "LittleCarDatabase.h"
@implementation CarNewsBase
+(NSString *)insertIntoCarNewsDetail:(CarNewsModel *)model{
    sqlite3 *dbPoint = [LittleCarDatabase openLittleCarDB];
    NSString *sqlstr = [NSString stringWithFormat:@"insert into CarNewsDetail (newsTitle,newsLink) values('%@','%@')",model.newsTitle,model.newsLink];
    //执行
    int result = sqlite3_exec(dbPoint, [sqlstr UTF8String], nil, nil, nil);
    NSString *string = @"";
    if (result == SQLITE_OK) {
        string = [NSString stringWithFormat:@"收藏成功%@",string];
    } else {
        string = [NSString stringWithFormat:@"已收藏%@",string];
    }
    return string;
}

+(NSMutableArray *)selectAllCarNewsDetail{
    sqlite3 *dbPoint = [LittleCarDatabase openLittleCarDB];
    //获得数据库指针的替身
    sqlite3_stmt *stmt = nil;
    //sql语句
    NSString *str = @"select newsTitle,newsLink from CarNewsDetail";
    int result = sqlite3_prepare_v2(dbPoint, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //获得每一条查找到的数据的内容
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *newsTitle = sqlite3_column_text(stmt, 0);
            const unsigned char *newsLink = sqlite3_column_text(stmt, 1);
            
            
            NSString *NewsTitle = [NSString stringWithUTF8String:(const char *)newsTitle];
            NSString *NewsLink = [NSString stringWithUTF8String:(const char *)newsLink];
            
            CarNewsModel *model = [CarNewsModel carNewsWithNewsTitle:NewsTitle newsLink:NewsLink];
            [array addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return array;
}




//查询一条信息
+(CarNewsModel *)selectWithNid:(NSString *)newsLink{
    sqlite3 *dbPoint = [LittleCarDatabase openLittleCarDB];
    //获得数据库指针的替身
    sqlite3_stmt *stmt = nil;
    //sql语句
    NSString *str = [NSString stringWithFormat:@"select newsTitle,%@ from CarNewsDetail",newsLink];
    int result = sqlite3_prepare_v2(dbPoint, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        //获得每一条查找到的数据的内容
        if  (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *newsTitle = sqlite3_column_text(stmt, 0);
            const unsigned char *newsLink = sqlite3_column_text(stmt, 1);
            
            NSString *NewsTitle = [NSString stringWithUTF8String:(const char *)newsTitle];
            NSString *NewsLink = [NSString stringWithUTF8String:(const char *)newsLink];
            
            CarNewsModel *model = [CarNewsModel carNewsWithNewsTitle:NewsTitle newsLink:NewsLink];
            sqlite3_finalize(stmt);
            return model;
        }
    }
    sqlite3_finalize(stmt);
    return nil;
}


+(void)deleteCarNewsDetailWithModel:(CarNewsModel *)model{
    sqlite3 *dbPoint = [LittleCarDatabase openLittleCarDB];
    NSString *sqlstr = [NSString stringWithFormat:@"delete from CarNewsDetail where newsLink = '%@'",model.newsLink];
    int result = sqlite3_exec(dbPoint, [sqlstr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog( @"删除失败");
    }
    
}

@end
